package com.zl.autocode.service.autocode.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeConfigC;
import com.zl.autocode.common.constant.AutocodeProperties;
import com.zl.autocode.dao.autocode.ZLCfgGenerateMapper;
import com.zl.autocode.dao.autocode.ZlDdlTemplateMapper;
import com.zl.autocode.dto.autocode.ddlTemplate.ReqDdlTemplateAddDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ReqDdlTemplateDelDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ReqDdlTemplateDetailDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ReqDdlTemplateEditDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ReqDdlTemplateQueryDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ReqDdlTemplateStateDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ResDdlTemplateAddDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ResDdlTemplateDelDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ResDdlTemplateDetailDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ResDdlTemplateEditDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ResDdlTemplateQueryDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ResDdlTemplateStateDTO;
import com.zl.autocode.dto.autocode.ddlTemplate.ResDdlTemplateVO2;
import com.zl.autocode.entity.autocode.ZlDdlTemplate;
import com.zl.autocode.entity.autocode.po.ZlDdlTemplateVo;
import com.zl.autocode.service.autocode.IDdlTemplateService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 建表sql模板
 * @author ZL
 */
@Service("autocodeDdlTemplateService")
public class DdlTemplateServiceImpl extends AutocodeBaseManager implements IDdlTemplateService {

    @Autowired
    private ZlDdlTemplateMapper ddlTemplateMapper;
    @Autowired
    private AutocodeProperties autocodeProperties;
    @Autowired
    private ZLCfgGenerateMapper cfgGenerateMapper;
    
    @Override
    public ResDdlTemplateQueryDTO queryDdlTemplateList(ReqDTO<ReqDdlTemplateQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDdlTemplateQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResDdlTemplateQueryDTO resDTO = BeanUtil.copyProperties(page, ResDdlTemplateQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getTemplateTitle())){
            reqData.setTemplateTitle("%"+reqData.getTemplateTitle()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlDdlTemplateVo> list = ddlTemplateMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResDdlTemplateVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResDdlTemplateDetailDTO queryDdlTemplateDetail(ReqDTO<ReqDdlTemplateDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDdlTemplateDetailDTO reqData = reqInfo.getReqData();
        ResDdlTemplateDetailDTO res = new ResDdlTemplateDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlDdlTemplate view = ddlTemplateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResDdlTemplateAddDTO insertDdlTemplateInfo(ReqDTO<ReqDdlTemplateAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDdlTemplateAddDTO reqData = reqInfo.getReqData();
        ResDdlTemplateAddDTO res = new ResDdlTemplateAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlDdlTemplate record = new ZlDdlTemplate();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setTemplateId(IdUtil.simpleUUID());
        record.setTemplatePath(filePath(autocodeProperties.getAutocodePathMajor(),autocodeProperties.getAutocodePathOther()
                ,new String[]{AutocodeConfigC.PATH_CHILDREN_DDL})+record.getTemplateId()+AutocodeConfigC.TEMPLATE_EXT_FTL);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateId",record.getTemplateId());
        //检查主键是否冲突
        if(ddlTemplateMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        ddlTemplateMapper.insertSelective(record);
        
        ZlDdlTemplate keyInfo = ddlTemplateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDdlTemplateEditDTO updateDdlTemplateInfo(ReqDTO<ReqDdlTemplateEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDdlTemplateEditDTO reqData = reqInfo.getReqData();
        ResDdlTemplateEditDTO res = new ResDdlTemplateEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlDdlTemplate record = new ZlDdlTemplate();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        ddlTemplateMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateId",record.getTemplateId());
        ZlDdlTemplate keyInfo = ddlTemplateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDdlTemplateDelDTO deleteDdlTemplateInfo(ReqDTO<ReqDdlTemplateDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDdlTemplateDelDTO reqData = reqInfo.getReqData();
        ResDdlTemplateDelDTO res = new ResDdlTemplateDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData);
        
        ZlDdlTemplate template = ddlTemplateMapper.selectOneByMap(paramMap);
        if(StringUtils.isNotBlank(template.getTemplatePath())
                && template.getTemplatePath().contains(autocodeProperties.getAutocodePathMajor()) 
                && template.getTemplatePath().contains(autocodeProperties.getAutocodePathTemplate())){
            FileUtil.del(handlerRootPath(template.getTemplatePath()));
        }
        ddlTemplateMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResDdlTemplateStateDTO updateDdlTemplateState(ReqDTO<ReqDdlTemplateStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqDdlTemplateStateDTO reqData = reqInfo.getReqData();
        ResDdlTemplateStateDTO res = new ResDdlTemplateStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlDdlTemplate record = new ZlDdlTemplate();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        ddlTemplateMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("templateId",record.getTemplateId());
        ZlDdlTemplate keyInfo = ddlTemplateMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResDdlTemplateDelDTO deleteDdlTemplateAndDetail(ReqDTO<ReqDdlTemplateDelDTO> reqInfo) throws Exception{
        return deleteDdlTemplateInfo(reqInfo);
    }
}